var express = require('express');
var router = express.Router();
var workService = require('../../service/export/WorkService')
var commonService = require('../../service/export/CommonExportService')

/**
 * 导出工作任务.
 */
router.post('/export/task', function (req, res, next) {
  var condition = req.body;
  workService.getExcel('key_task', condition).then((workbook) => {
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=key_task.xlsx");
    workbook.xlsx.write(res).then(() => {
        res.end();
    }).catch((err) => {
      res.send(err)
    })
  })
});

/**
 * 通用导出.
 * params {
 *   tableName: "",
 *   enumTypes: [],
 *   tableConfigs: []
 *   header: [
 *     { title: '序号', width: 10,  },
 *     { title: '序号', width: 10, ext: { prop: 'userName', type: 'index|enum|table|default|custom|date', func: '', key: 'BUG_REASON', value: 'email', label: 'userName' } }
 *   ],
 *   condition: {}
 * }
 */
router.post('/common/export', function (req, res, next) {
  var params = req.body;
  console.log(params);
  commonService.getExcel(params).then((workbook) => {
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=default.xlsx");
    workbook.xlsx.write(res).then(() => {
      res.end();
    }).catch((err) => {
      res.send(err)
    })
  })
});

module.exports = router;